﻿using DbFactory;
using PlaneClean.CommonHelp;
using PlaneClean.Models;
using PlaneClean.Service;
using PlaneClean.ViewModels.Menu;
using PlaneCleanEntity;
using PlaneCleanEntity.ViewEntity.SelectTreeEntity;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// 角色菜单管理
    /// </summary>
    public class MenuGroupController : ManagerController
    {
        /// <summary>
        /// 根据角色id查询菜单树
        /// </summary>
        /// <param name="groupId">角色id</param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<v_t_menu> List(int groupId)
        {
            ListResponse<v_t_menu> response = new ListResponse<v_t_menu>();
            response.bodyList = new List<v_t_menu>();
            response.result = "NG";
            List<t_group_menu> gmList = dbFactory.Db.Queryable<t_group_menu>().Where(a => a.g_id == groupId).ToList();
            List<int> menuIdList = gmList.Select(a => a.m_id).ToList();
            List<v_t_menu> menuList = dbFactory.Db.Queryable<t_menu>().Where(a => menuIdList.Contains(a.m_id)).Select<v_t_menu>().ToList();
            foreach(var menu in menuList)
            {
                menu.checkButtens = new List<string>();
                string buttens = gmList.Where(a => a.m_id == menu.m_id).FirstOrDefault().m_buttens;
                if (!string.IsNullOrEmpty(buttens))
                {
                    menu.checkButtens = buttens.Split(',').ToList();
                }
               
            }
            MenuSerivce ms = new MenuSerivce();
            response.bodyList = ms.ToTreeMenu(menuList, 0);
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 更新菜单角色按钮权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Update(BaseRequest<List<t_group_menu>> request)
        {
            BaseResponse response = new BaseResponse();
            if (request.body == null || request.body.Count < 0)
            {
                response.reason = "请传入需要设置的角色菜单";
                return response;
            }
            foreach(var item in request.body)
            {
                dbFactory.Db.Updateable<t_group_menu>().SetColumns(a => new t_group_menu() { m_buttens = item.m_buttens })
                    .Where(a => a.m_id == item.m_id && a.g_id == item.g_id).ExecuteCommand();
            }
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 新增角色菜单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Add(AddRequest request)
        {

            BaseResponse response = new BaseResponse();
            //response.reason = request.idList.Count.ToString();

            if (request.g_id <= 0)
            {
                response.reason = "请传入需要新增用户角色的主键";
                return response;
            }
            if (request.m_id == null || request.m_id.Count <= 0)
            {
                response.reason = "请传入需要新增角色用户的主键";
                return response;
            }
            List<int> hasChildMId = new List<int>();
            List<int> noChildMId = new List<int>();
            List<t_group_menu> gmList = DbFactory.dbFactory.Db.Queryable<t_group_menu>().Where(a => a.g_id == request.g_id && request.m_id.Contains(a.m_id)).ToList();
            if (gmList != null && gmList.Count > 0)
            {
                foreach (var gmTemp in gmList)
                {
                    t_menu Childmenu = dbFactory.Db.Queryable<t_menu>().Where(a => a.m_parent_id == gmTemp.m_id).First();
                    if (Childmenu != null)
                    {
                        hasChildMId.Add(gmTemp.m_id);
                        continue;
                    }
                    noChildMId.Add(gmTemp.m_id);
                }
            }
            t_menu menu = new t_menu();
            if (noChildMId != null && noChildMId.Count > 0)
            {
                menu = dbFactory.Db.Queryable<t_menu>().Where(a => noChildMId.Contains(a.m_id)).First();
                response.reason = "角色已存在菜单:" + menu.m_name + ",新增失败";
                return response;
            }
            t_group_menu gm = new t_group_menu();

            foreach (var item in request.m_id)
            {
                bool insert = true;
                if (hasChildMId != null && hasChildMId.Count > 0)
                {
                    foreach (var cmid in hasChildMId)
                    {
                        if (cmid == item)
                        {
                            insert = false;
                            break;
                        }
                    }
                }
                if (!insert)
                {
                    continue;
                }
                gm.g_id = request.g_id;
                gm.m_id = item;
                DbFactory.dbFactory.Db.Insertable(gm).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity();
            }
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Delete(DeleteRequest request)
        {
            BaseResponse response = new BaseResponse();
            //response.reason = request.idList.Count.ToString();

            if (request.groupId <= 0)
            {
                response.reason = "请传入需要删除用户角色的主键";
                return response;
            }
            if (request.idList == null || request.idList.Count <= 0)
            {
                response.reason = "请传入需要删除角色用户的主键";
                return response;
            }
            List<t_group_menu> gmList = DbFactory.dbFactory.Db.Queryable<t_group_menu>().Where(a => a.g_id == request.groupId && request.idList.Contains(a.m_id)).ToList();
            if (gmList != null && gmList.Count > 0)
            {
                foreach (var gmTemp in gmList)
                {
                    if (gmTemp.g_id == 1)
                    {
                        response.reason = "系统管理员的菜单权限不能删除";
                        return response;
                    }

                    t_menu Childmenu = dbFactory.Db.Queryable<t_menu>().Where(a => a.m_parent_id == gmTemp.m_id).First();
                   
                    if (Childmenu != null)
                    {
                        t_menu menu = dbFactory.Db.Queryable<t_menu>().Where(a => gmTemp.m_id == a.m_id).First();
                        response.reason = "菜单:"+ menu.m_name+",存在子菜单权限,删除失败";
                        return response;
                    }
                }
            }
            dbFactory.Db.Deleteable<t_group_menu>().Where(a => a.g_id == request.groupId && request.idList.Contains(a.m_id)).ExecuteCommand();
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<SelectTree> SelectTree()
        {
            ListResponse<SelectTree> response = new ListResponse<SelectTree>();
            response.bodyList = new List<SelectTree>();
            response.result = "NG";
            List<t_menu> menuList = dbFactory.Db.Queryable<t_menu>().Where(a => a.m_state == 1).ToList();
            CommonTreeHelp<t_menu, SelectTree> cth = new CommonTreeHelp<t_menu, SelectTree>();
            response.bodyList = cth.ToTreeMenu(menuList, 0, "m_id", "m_name", "m_parent_id");
            response.result = "OK";
            return response;
        }
    }
}